using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;


public class FadeSprite : MonoBehaviour
{
    SpriteRenderer spriteRenderer;
    private Sprite original_sprite;
    private Texture2D original_texture;
    private Animator _animator;

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        original_sprite = spriteRenderer.sprite;
        // BdPackage.BdUtils.log($"--- original_sprite: {original_sprite.name}");
        original_texture = spriteRenderer.sprite.texture;
        _animator = GetComponent<Animator>();
    }

    public void btn_break_shield()
    {
        _animator.SetTrigger("break");
    }

    private void OnEnable()
    {
        btn_fade_in();
        // spriteRenderer = GetComponent<SpriteRenderer>();
        spriteRenderer.sprite = original_sprite;
        // BdPackage.BdUtils.log($"--- OnEnable --- original_sprite: {original_sprite.name}");
        // BdPackage.BdUtils.log($"--- OnEnable --- spriteRenderer.sprite: {spriteRenderer.sprite.name}");

        Sprite newSprite = Sprite.Create(
            original_texture,
            new Rect(0, 0, original_texture.width, original_texture.height),
            new Vector2(0.5f, 0.5f) // Pivot 点
        );
        spriteRenderer.sprite = newSprite;

        // spriteRenderer.sprite.texture = original_texture;
    }

    public void btn_fade_in()
    {
        // spriteRenderer = GetComponent<SpriteRenderer>();
        spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 1);
    }
    public void btn_fade_out(float duration = 0.2f)
    {
        // spriteRenderer = GetComponent<SpriteRenderer>();
        // get spriteRenderer, and use dotween to implement fade effect
        spriteRenderer.DOFade(0, duration).OnComplete(() =>
        {
            // Destroy(gameObject);
        });
    }
}
